package haoji_juc;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
 * @program: Dome
 * @description:
 * @className: TestCallable1
 * @author: rjt99999
 * @version: 1.0.0
 * @create: 2020-08-03 20:13
 **/
public class TestCallable1 {

    /*
            使用submit方法还有一个特点就是，他的异常可以在主线程中catch到。

            而使用execute方法执行任务是捕捉不到异常的。
     */


    public static void main(String[] args) {

        ExecutorService executorService = Executors.newFixedThreadPool(2);


        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                //抛出异常
                executorService.execute(null);

            }
        };

        //捕获不到异常
//        try {
//            executorService.execute(runnable);
//        } catch (Exception e) {
//            e.printStackTrace();
//            System.out.println("e = " + e.getMessage());
//        }


        Callable<Integer> callable = new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {


                return 1/0;

            }
        };

        //可获取异常

        try {
            executorService.submit(callable).get();
        } catch (Exception e) {
            e.printStackTrace();

            System.out.println("e.getMessage() = " + e.getMessage());
        }

        executorService.shutdownNow();
    }

}
